<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */

if (!defined('PMA_MYSQL_WRAPPERS_LIB_INCLUDED')){
    define('PMA_MYSQL_WRAPPERS_LIB_INCLUDED', 1);

    function PMA_mysql_error($id = FALSE) {
        if ($id != FALSE) {
            if (mysql_errno($id) != 0) return mysql_errno($id).' - '.mysql_error($id);
        } else {
            if (mysql_errno() != 0) return mysql_errno().' - '.mysql_error();
        }
		return FALSE;
    }

    function PMA_mysql_fetch_array($result, $type = FALSE) {
        if ($type != FALSE) {
            return mysql_fetch_array($result, $type);
        } else {
            return mysql_fetch_array($result);
        }
    }

    function PMA_mysql_fetch_field($result , $field_offset = FALSE) {
        if ($field_offset != FALSE) {
            return mysql_fetch_field($result, $field_offset);
        } else {
            return mysql_fetch_field($result);
        }
    }

    function PMA_mysql_fetch_object($result) {
        return mysql_fetch_object($result);
    }

    function PMA_mysql_fetch_row($result) {
        return mysql_fetch_row($result);
    }

    function PMA_mysql_field_flags($result, $field_offset) {
        return mysql_field_flags($result, $field_offset);
    }

    function PMA_mysql_field_name($result, $field_index) {
        return mysql_field_name($result, $field_index);
    }

    function PMA_mysql_field_type($result, $field_index) {
        return mysql_field_type($result, $field_index);
    }

    function PMA_mysql_num_rows($result) {
        return mysql_num_rows($result);
    }

    function PMA_mysql_list_fields($database_name, $table_name, $link_identifier = FALSE) {
        if ($link_identifier != FALSE) {
            return mysql_list_fields($database_name, $table_name, $link_identifier);
        } else {
            return mysql_list_fields($database_name, $table_name);
        }
    }

    function PMA_mysql_list_tables($database_name, $link_identifier = FALSE) {
        if ($link_identifier != FALSE) {
            return mysql_list_tables($database_name, $link_identifier);
        } else {
            return mysql_list_tables($database_name);
        }
    }

    function PMA_mysql_query($query, $link_identifier = FALSE, $result_mode = FALSE) {
		if(PMA_mysql_isOnRepair()){
			if(!preg_match("/bet_LoginLog|bet_Sessions/", $query)){
				if(preg_match("/update.+set|insert.+into|delete.+from/", $query)){
					$GLOBALS["errormessage"] = "DB 維護中，暫停各種資料修改，請見諒！ 22:00 以後重新開放！"; //DB 維護中，暫停各種資料修改，請見諒！ 22:00 以後重新開放！
					$GLOBALS["errorhidden"] = "Error:".$query."\nLine:".__LINE__."\nFile:".__FILE__;
					ErrMsgOutputHTML("LoginError", "", 1);
				}
			}
		}
		//$initTime = time();
        if ($link_identifier != FALSE) {
            if ($result_mode != FALSE) {
                $intRS = mysql_query($query, $link_identifier, $result_mode);
            } else {
                $intRS = mysql_query($query, $link_identifier);
            }
        } else {
            $intRS = mysql_query($query);
        }
		/*$initTime = time() - $initTime;
		if($initTime >= 1){
			$strLog = "Date: ".date("y-m-d H:i:s")." => Sec: ".$initTime." => ".$query."\n";
			$fp = fopen("/home/share/tmp/SlowSQL_".$_SERVER["HTTP_HOST"].".txt","a+");
			fputs($fp, $strLog);
			fclose($fp);
		}*/
		return $intRS;
    }

    function PMA_mysql_result($result, $row, $field = FALSE) {
        if ($field != FALSE) {
            return mysql_result($result, $row, $field);
        } else {
            return mysql_result($result, $row);
        }
    }

    function PMA_mysql_select_db($database_name, $link_identifier = FALSE) {
        if ($link_identifier != FALSE) {
            return mysql_select_db($database_name, $link_identifier);
        } else {
            return mysql_select_db($database_name);
        }
    }

    function PMA_mysql_tablename($result, $i) {
        return mysql_tablename($result, $i);
    }

	function DBConnection($strDBSite){
		global $_blDBHostQ, $_blDBHostM, $_blDBHostOld, $_blDBUser, $_blDBPass, $_blDBName, $_Conn;
		switch($strDBSite){	
			case "Query":	//查詢資料庫
			case "Result":	//結帳資料庫
				$_Conn = "_ConnQuery";
				$dbHost = $_blDBHostQ;
				break;

			case "History":	//歷史資料庫
				$_Conn = "_ConnQuery";
				$dbHost = $_blDBHostOld;
				break;

			case "Master":	 //主資料庫
			default:	 //預設
				$_Conn = "_ConnMaster";
				$dbHost = $_blDBHostM;
				break;
		}

		$arrError = array();
		if (! $GLOBALS[$_Conn] = @mysql_connect($dbHost, $_blDBUser, $_blDBPass)) {
			$arrError["Subject"] = " Connect Error!";
			$arrError["Content"] = "連線錯誤：無法連線至資料庫主機！\nDBSite".$strDBSite."\nPage: ".$_SERVER["PHP_SELF"]."\n";
			return $arrError;
		}

		PMA_mysql_query("SET NAMES 'utf8'");
		if (! @PMA_mysql_select_db($_blDBName, $GLOBALS[$_Conn])) {
			$arrError["Subject"] = " Select DB Error!";
			$arrError["Content"] = "連線錯誤：無法連結資料庫！\nDBSite".$strDBSite."\nPage: ".$_SERVER["PHP_SELF"]."\n".PMA_mysql_error($GLOBALS[$_Conn]);
			return $arrError;
		}

		return $arrError;
	}


	function OtherDBConnection(&$OConn, $strHost, $strDBUser, $strDBPass, $strDBName){

		$arrError = array();
		if (! $OConn = @mysql_connect($strHost, $strDBUser, $strDBPass)) {
			$arrError["Subject"] = " Connect Error!";
			$arrError["Content"] = "連線錯誤：無法連線至資料庫主機！\nHost:".$strHost."\nPage: ".$_SERVER["PHP_SELF"]."\n";
			$arrError["Content"] .= "\n".'<!--'.mysql_error().'//-->';
			return $arrError;
		}

		if (! @PMA_mysql_select_db($strDBName, $OConn)) {
			$arrError["Subject"] = " Select DB Error!";
			$arrError["Content"] = "連線錯誤：無法連結資料庫！\nHost:".$strHost."\nPage: ".$_SERVER["PHP_SELF"]."\n".PMA_mysql_error($OConn);
			return $arrError;
		}

		return $arrError;
	}

    function PMA_mysql_isOnRepair() {
		global $_BetDBName;
		//暫不維修的 DB
		switch($_BetDBName){
			case "betTienUSBet33us":
			case "bet3DUSBet33us":
			case "betNewUSWinus": //新世紀
			case "betLonUSWinus": //萬隆
			case "betUnUSWinus":  //永盛
				return false;
				break;
		}
		return false;	//正常營運
        //return true;	//維護中
    }

    function PMA_mysql_OnRepair_DBHost($strDBName) {
		$arrDBHost = array(
			"betUS99us"			=> "192.168.5.211:3307",
			"betRiseUSWinus"	=> "192.168.5.211:3308",
			"betUS77us"			=> "192.168.5.211:3309",
			"betPortUSWinus"	=> "192.168.5.211:3311",
			"betUSBet99us"		=> "192.168.5.212:3312",
			//"bet3DUSBet33us"	=> "192.168.5.212:3313",
			"betUSWin99us"		=> "192.168.5.212:3314",
			"betUSBet99us"		=> "192.168.5.212:3315",
			"betEastUSWinus"	=> "192.168.5.212:3316",
			"betUS168net"		=> "192.168.5.212:3317",
			"betUSWin88us"		=> "192.168.5.212:3318",
			//"betLonUSWinus"		=> "192.168.5.212:3319",
			"betSkyUSWinus"		=> "192.168.5.212:3320",
			"betNewUSWinus"		=> "192.168.5.212:3321",
			"betBoUSWinus"		=> "192.168.5.213:3322",
			"betUSBet767us"		=> "192.168.5.213:3323",
			"betUSWin66us"		=> "192.168.5.213:3324",
			"betStartUSWinus"	=> "192.168.5.213:3326",
			"betDaUSWinus"		=> "192.168.5.213:3328",
			//"betTienUSBet33us"	=> "192.168.5.212:3313",
			"betWorldUSWinus"	=> "192.168.5.212:3314",
			"betFaUSWinus"		=> "192.168.5.212:3316",
			//"betUnUSWinus"		=> "192.168.5.212:3319",
			"betHunUSWinus"		=> "192.168.5.213:3322",
			"betStartUSWinus"	=> "192.168.5.213:3326",
			"betUnion168net"	=> "192.168.5.213:3328",
			"betUS33us"			=> "192.168.5.213:3322",
			"betStartUSWinus"	=> "192.168.5.213:3326"
		);
		//exit($arrDBHost[$strDBName]);
        return $arrDBHost[$strDBName];
    }
} // PMA_MYSQL_WRAPPERS_LIB_INCLUDED
?>